var mysql_credential = require('./credentials');
var mysql = require('mysql');
var db = {};
var pool = mysql.createPool(mysql_credential.mysql);



db.query = function(sqlan) {
    return new Promise((resolve, reject) => {
        let sql = sqlan;
        if(!sql) reject(new Error('give me a sql'));
        pool.query(sql, function (err, results, fields) {
            if(err)reject(err);
            resolve(results)
        })
    }).then(result=>result).catch(err=>new Error(err))
};

db.insert = function insertBack(sql1,sql2, fn) {
    let sql = sql1;
    if(!sql) return;
    connection.query(sql1, sql2, function (err, rows) {
        if(err) {
            console.log(err);
            return
        }
        fn(rows)
    })
}
module.exports = db;